//
//  IndexHeaderView.m
//  wozhua
//
//  Created by 王洋 on 16/5/10.
//  Copyright © 2016年 王洋. All rights reserved.
//

#import "IndexHeaderView.h"
#import "SWQRCode.h"

@interface IndexHeaderView ()

@end

@implementation IndexHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
 
    self.bgView1.hidden = NO;
    self.phoneTextField.clearButtonMode = UITextFieldViewModeAlways;
}

//扫一扫
- (IBAction)didClickedRecodeButton:(id)sender {
    SWQRCodeConfig *config = [[SWQRCodeConfig alloc]init];
    config.scannerType = SWScannerTypeBoth;
    
    SWQRCodeViewController *qrcodeVC = [[SWQRCodeViewController alloc]init];
    qrcodeVC.codeConfig = config;
    [self.firstViewController.navigationController pushViewController:qrcodeVC animated:YES];
    
    WEAKSELF
    qrcodeVC.QRcodeScan = ^(NSString *resultStr) {
        [weakSelf queryMothod:1 codeStr:resultStr];
    };
}

//查询
- (IBAction)didClickedQueryButton:(id)sender {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    
    if (self.phoneTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    
    [self queryMothod:2 codeStr:self.phoneTextField.text];
}

- (void)queryMothod:(NSInteger)type codeStr:(NSString *)codeStr {
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:codeStr forKey:@"flag"];
    [parameters setObject:@(type) forKey:@"type"];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在查询..."];
    [NetworkHelper apiPost:@"manager/getUserInfo" parameters:parameters completion:^(id info, NSError *error) {
        [SVProgressHUD dismiss];
        if (!error) {
            if ([[info objectForKey:@"sta"] integerValue] == 1) {
                [[UserInfoManager sharedManager] saveUserInfo:[info objectForKey:@"response"]];
                weakSelf.phoneTextField.text = @"";
                weakSelf.bgView1.hidden = YES;
                if (weakSelf.querySuccessBlock) {
                    weakSelf.querySuccessBlock();
                }
                
                weakSelf.nameLabel.text = UserInfo.name;
                weakSelf.totalLabel.text = [NSString stringWithFormat:@"  总积分 %@  " , UserInfo.amount_point];
                weakSelf.scoreLabel.text = [NSString stringWithFormat:@"  可用积分 %@  " , UserInfo.point];
                weakSelf.mobileLabel.text = UserInfo.mobile;
                weakSelf.idCodeLabel.text = UserInfo.id_code;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"QuerySuccessNotification" object:nil];
            } else {
                [SVProgressHUD showErrorWithStatus:[info objectForKey:@"remarks"]];
            }
        }
    }];
}

//退出
- (IBAction)didClickedSignOutButton:(id)sender {
    [[UserInfoManager sharedManager] removeUserInfoData];
    self.bgView1.hidden = NO;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"QuerySingOutNotification" object:nil];
}

@end
